package com.my.blog.controller;

import com.my.blog.domain.ResponseResult;
import com.my.blog.domain.entity.User;
import com.my.blog.domain.vo.AdminUserInfoVo;
import com.my.blog.domain.vo.MenuVo;
import com.my.blog.domain.vo.UserInfoVo;
import com.my.blog.enums.AppHttpCodeEnum;
import com.my.blog.exception.SystemException;
import com.my.blog.service.IAdminLoginService;
import com.my.blog.service.IMenuService;
import com.my.blog.service.IRoleService;
import com.my.blog.utils.BeanCopyUtils;
import com.my.blog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class AdminLoginController {

    @Autowired
    private IAdminLoginService adminLoginService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IRoleService roleService;


    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user) {
        if (!StringUtils.hasText(user.getUserName())) {
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return adminLoginService.login(user);
    }

    @GetMapping("/getInfo")
    public ResponseResult getInfo(){
        // 获取当前登录的用户信息
        User user = SecurityUtils.getLoginUser().getUser();
        // 根据用户id获取权限信息
        List<String> perms = menuService.selectPermsByUserId(user.getId());
        // 根据用户id获取角色信息
        List<String> roles = roleService.selectRoleKeyByUserId(user.getId());
        // 封装数据
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms, roles, userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    @GetMapping("/getRouters")
    public ResponseResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        //查询menu，返回的menus以tree形式表示父子菜单的层级关系
        List<MenuVo> menus = menuService.selectRouterMenuTreeByUserId(userId);

        //封装数据返回
        HashMap<Object, Object> map = new HashMap<>();
        map.put("menus", menus);
        return ResponseResult.okResult(map);
    }

    @PostMapping("/user/logout")
    public ResponseResult logout(){
        return adminLoginService.logout();
    }
}
